#!/bin/bash -e

cd `dirname $0`
top_srcdir=`pwd`
script_name=./`basename $0`

#
# Script to check versions of dependencies
# To check the version for all dependencies, just run the script with no arguments.
# To check the version for a specific dependency, run the script with "check <dependency>"
# To reset a dependency to the needed version, run the script with "reset <dependency>"
# It is also possible to pass "reset all" to reset all dependencies
#

# md-addins
DEP[0]=md-addins
DEP_NAME[0]=MDADDINS
DEP_PATH[0]=${top_srcdir}/../md-addins
DEP_MODULE[0]=git@github.com:xamarin/md-addins.git
DEP_NEEDED_VERSION[0]=cc1e7e1bd40987e36d86515e206f59b050f5569f
DEP_BRANCH_AND_REMOTE[0]="master origin/master"

# other dependencies
# DEP[1]=...
# ...

function fetch_variables
{
	IGNORE_VERSION=`eval echo \\\$IGNORE_${DEP_NAME[$1]}_VERSION`
	THE_PATH=${DEP_PATH[$1]}
	NAME=${DEP[$1]}
	NEEDED_VERSION=${DEP_NEEDED_VERSION[$1]}
	if test -d "$THE_PATH"; then
		cd "$THE_PATH"
		VERSION=`git log | head -1 | awk '{print $2}' 2>/dev/null`
		BRANCH=`git status | head -1 | awk '{print $4}' 2>/dev/null`
		cd "$top_srcdir"
	else
		VERSION=
		BRANCH=
	fi
	BRANCH_AND_REMOTE=${DEP_BRANCH_AND_REMOTE[$1]}
	NEEDED_BRANCH=`echo "$BRANCH_AND_REMOTE" | sed -e 's, .*,,'`
	MODULE=${DEP_MODULE[$1]}
}

function reset_version
{
	fetch_variables $1

	if test -d "$THE_PATH"; then
		cd "$THE_PATH"
		if ! git show "$NEEDED_VERSION" >/dev/null 2>&1; then
			echo "*** [$NAME] git fetch $NAME"
			git fetch
		fi
	else
		echo "*** [$NAME] git clone $MODULE"
		cd "`dirname $THE_PATH`"
		git clone $MODULE
	fi

	cd "$THE_PATH"
	echo "*** [$NAME] git checkout $NEEDED_BRANCH"
	git checkout $NEEDED_BRANCH || git checkout -b $BRANCH_AND_REMOTE
	echo "*** [$NAME] git reset --hard $NEEDED_VERSION"
	(git reset --hard $NEEDED_VERSION && git submodule update --init --recursive) || exit 1;

	cd "$top_srcdir"
}

function check_version
{
	fetch_variables $1

	if [[ "x$IGNORE_VERSION" == "x" ]]; then
		if test ! -d "$THE_PATH"; then
			echo "Your $NAME checkout is missing, please run '$script_name --reset $NAME'"
			FAILURE=1
		else
			if [[ "x$VERSION" != "x$NEEDED_VERSION" || "x$BRANCH" != "x$NEEDED_BRANCH" ]]; then
				echo "Your $NAME version is out of date, please run '$script_name --reset $NAME'"
				FAILURE=1
			fi
		fi
	fi
}

function reset_all
{
	I=0
	while [[ "x${#DEP[@]}" != "x$I" ]]; do
		reset_version $I
		let I=$I+1
	done
}

function check_all
{
	I=0
	while [[ "x${#DEP[@]}" != "x$I" ]]; do
		check_version $I
		let I=$I+1
	done
}

function find_dep
{
	I=0
	DEP_IDX=
	while [[ "x${#DEP[@]}" != "x$I" ]]; do
		if [[ "x${DEP[$I]}" == "x" ]]; then
			echo "There is no dependency named $1"
			exit 1
		elif [[ "x${DEP[$I]}" == "x$1" ]]; then
			DEP_IDX=$I
			break;
		fi
		let I=$I+1
	done
	if [[ "x$DEP_IDX" == "x" ]]; then
		echo "There is no dependency named $1"
		exit 1
	fi
}

FAILURE=0

if [[ "x$1" == "x" ]]; then
	check_all
elif [[ "x$1" == "x--check-all" ]]; then
	check_all
elif [[ "x$1" == "x--check" && "x$2" == "xall" ]]; then
	check_all
elif [[ "x$1" == "x--check" && "x$2" == "x" ]]; then
	check_all
elif [[ "x$1" == "x--check" ]]; then
	shift
	while [[ "x$1" != "x" ]]; do
		find_dep $1
		check_version $DEP_IDX
		shift
	done
elif [[ "x$1" == "x--reset-all" ]]; then
	reset_all
elif [[ "x$1" == "x--reset" && "x$2" == "xall" ]]; then
	reset_all
elif [[ "x$1" == "x--reset" && "x$2" == "x" ]]; then
	reset_all
elif [[ "x$1" == "x--reset" ]]; then
	shift
	while [[ "x$1" != "x" ]]; do
		find_dep $1
		reset_version $DEP_IDX
		shift
	done
else
	echo Unknown arguments: $@
	echo 'Expected --reset|--check <list of dependencies>'
	exit 1
fi

if [[ "x$FAILURE" == "x1" ]]; then
	echo "One or more modules needs update"
	exit 1
else
	echo "All dependent modules up to date"
fi
